package com.niuniu;

import com.xingli.zhajinhua.beans.Card;
import com.xingli.zhajinhua.beans.zhajinhua.enums.ZhaCardColor;
import com.xingli.zhajinhua.beans.zhajinhua.enums.ZhaCardPoints;
import com.xingli.zhajinhua.beans.zhajinhua.enums.ZhaCardType;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ZhaJinHuaLogic extends AbstractCardsGame{

    public ZhaJinHuaLogic(int playerNumber, int playerCardNumber, int baseWeight) {
        super(playerNumber, playerCardNumber, baseWeight);
    }

    public ZhaJinHuaLogic() {
        super(3, 3, 64);
    }
    @Override
    public List<Card> getNewCardCollectionAndShuffle() {
        ZhaCardColor[] cardColors = ZhaCardColor.values();
        ZhaCardPoints[] cardPoints = ZhaCardPoints.values();
        List<Card> cardList = new ArrayList<>(cardColors.length * cardPoints.length);
        for (ZhaCardColor color : cardColors) {
            for (ZhaCardPoints points : cardPoints) {
                int weight = ((points.weight - 1) * (cardColors.length) + color.weight);
                cardList.add(new Card(points.ordinal(), color.ordinal(), points.weight, color.weight, weight));
            }
        }
        Collections.shuffle(cardList);
        return cardList;
    }
    @Override
    public String judgeCardType(Card[] cards) {
        if ((cards[0].getPointWeight() == cards[1].getPointWeight())
                &&(cards[0].getPointWeight()==cards[2].getPointWeight())){
            
            return ZhaCardType.Baozi.name();
        }else if ((cards[0].getPointWeight()==cards[1].getPointWeight())
                ||(cards[1].getPointWeight()==cards[2].getPointWeight())){
            
            return ZhaCardType.DuiZi.name();
        }else if ((cards[0].getColorWeight() == cards[1].getColorWeight())
                &&(cards[0].getColorWeight() == cards[2].getColorWeight())
                &&(cards[2].getPointWeight()-cards[1].getPointWeight() == 1)
                &&(cards[1].getPointWeight()-cards[0].getPointWeight() == 1)){
            
            return ZhaCardType.TongHuaShun.name();
        }else if ((cards[0].getColorWeight() == cards[1].getColorWeight())
                && (cards[0].getColorWeight() == cards[2].getColorWeight())){
            if (cards[0].getPoint()==0 && cards[1].getPoint()==1&& cards[2].getPoint()==12){
                //是A23
                return ZhaCardType.TongHuaShun.name();
            }
            return ZhaCardType.Tonghua.name();
        }else if ((cards[2].getPointWeight()-cards[1].getPointWeight() == 1)
                && (cards[1].getPointWeight()-cards[0].getPointWeight() == 1) ){
            return ZhaCardType.ShunZi.name();
        }else {
            if (cards[0].getPoint()==0 && cards[1].getPoint()==1&& cards[2].getPoint()==12){
                //是A23
                return ZhaCardType.ShunZi.name();
            }
            return ZhaCardType.DanPai.name();
        }
    }

    @Override
    public int calculateCardsWeight(Card[] cards, String cardType) {
        int cardsWeight=0;
        ZhaCardType type = ZhaCardType.valueOf(cardType);
        if (type == ZhaCardType.DuiZi){
            //由于对子时可能单牌在最前面 所以做特殊处理
            if(cards[0].getPointWeight() == cards[1].getPointWeight()){
                //每张牌点数的权重加上单排的花色
                cardsWeight = cards[2].getPointWeight()*64 + cards[0].getPointWeight()*64*64+cards[2].getColorWeight();
            }else {
                //每张牌点数的权重加上单排的花色
                cardsWeight = cards[0].getPointWeight()*64 + cards[2].getPointWeight()*64*64+cards[0].getColorWeight();
            }
            cardsWeight = (int) (cardsWeight+type.weight*Math.pow(baseWeight,3));
        }else {

            for (int j = 0; j < cards.length; j++) {
                //每张牌点数对应的权重加上单排对应的权重
                cardsWeight+=cards[j].getPointWeight()*Math.pow(baseWeight,j)+cardsWeight*j;
//                cardsWeight+=cards[j].getWeight()*Math.pow(baseWeight,j);
            }
            //所有的权重
            cardsWeight = (int) (cardsWeight+type.weight*Math.pow(baseWeight,3));
        }
        return cardsWeight;
    }

}
